package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.entity.EnterpriseEmployeeRecord
import cn.exrick.xboot.base.entity.SecurityAdvisorJobTrainPart
import cn.exrick.xboot.base.mapper.SecurityAdvisorJobTrainPartMapper
import cn.exrick.xboot.base.service.*
import cn.exrick.xboot.base.vo.SecurityAdvisorJobTrainPartVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.concurrent.CompletableFuture
import javax.print.attribute.standard.JobName

/**
 * 安全顾问岗位培训分支接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class ISecurityAdvisorJobTrainPartServiceImpl : ServiceImpl<SecurityAdvisorJobTrainPartMapper?, SecurityAdvisorJobTrainPart?>(), ISecurityAdvisorJobTrainPartService {


    @Autowired
    private lateinit var iEnterpriseEmployeeRecordService: IEnterpriseEmployeeRecordService

    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iSecurityAdvisorJobTrainService: ISecurityAdvisorJobTrainService

    @Autowired
    private lateinit var iEnterpriseCommonParamService: IEnterpriseCommonParamService


    override fun createOrUpdateTrainPart(securityAdvisorJobTrainPart: SecurityAdvisorJobTrainPart): Result<Any?>? {

        return try {

            saveOrUpdate(securityAdvisorJobTrainPart)
            ResultUtil.success<Any>("操作成功")
        }catch (e: Exception) {

            ResultUtil.error<Any>(e.message)
        }
    }

    override fun removeTrainPart(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success<Any>("操作成功")
        }catch (e: Exception) {

            ResultUtil.error<Any>(e.message)
        }
    }

    override fun getTrainPartVo(id: String): SecurityAdvisorJobTrainPartVo? {

        return getById(id).run {

            SecurityAdvisorJobTrainPartVo().apply {

                ToolUtil.copyProperties(this@run, this)
                completeTrain = iEnterpriseEmployeeRecordService.count(KtQueryWrapper(EnterpriseEmployeeRecord::class.java)
                        .eq(EnterpriseEmployeeRecord::createBy, securityUtil.currUser.id)
                        .eq(EnterpriseEmployeeRecord::TestType, 2)
                        .eq(EnterpriseEmployeeRecord::completeTrain, true)) > 0

                simulationExamHighScore = iEnterpriseEmployeeRecordService.getOne(KtQueryWrapper(EnterpriseEmployeeRecord::class.java)
                        .eq(EnterpriseEmployeeRecord::TestType, 1)
                        .eq(EnterpriseEmployeeRecord::createBy, securityUtil.currUser.id)
                        .orderByDesc(EnterpriseEmployeeRecord::TestScore)
                        .last("limit 1"))?.TestScore ?: 0

                iEnterpriseCommonParamService.getEnterpriseCommonParamLast()?.let {

                    trainingPassMoney = it.trainingPassMoney
                    trainingNotPassMoney = it.trainingNotPassMoney
                }

            }

        }
    }

    override fun getTrainPartDetail(id: String?): Result<SecurityAdvisorJobTrainPartVo?>? {

        return getById(id)?.run {

            ResultUtil.data(getTrainPartVo(id!!))
        } ?: ResultUtil.error(201,"不存在当前培训")
    }

    override fun getTrainPartList(pageVo: PageVo, jobTrainId: String): Result<MutableMap<String, Any?>>? {

        return list(KtQueryWrapper(SecurityAdvisorJobTrainPart::class.java).eq(SecurityAdvisorJobTrainPart::jobTrainId, jobTrainId)
                .orderByDesc(SecurityAdvisorJobTrainPart::createTime)).run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getTrainPartVo(it?.id!!) }.toMutableList())
            result["jobTrainingDetail"] = iSecurityAdvisorJobTrainService.getJobTrainVo(jobTrainId)
            ResultUtil.data(result, "获取数据成功")
        }
    }
}